C++Data StructuresAlgorithmsCompetitive ProgrammingJavaPythonMicroprocessorsGraph TheoryComputer System ArchitectureMachine LearningArtificial IntelligenceData Structures in PythonJavascriptMySQLAndroid DevelopmentAlgorithms in PythonCoding InterviewData ScienceData Structures in JavaObject Oriented DesignLinked ListBinary Trees

Algorithms in Python

[1.1] Linear Search

[1.2] Binary Search (Iterative)

[1.3] Binary Search (Recursive)

[1.4] Selection Sort

[1.5] Heap Sort in Python

[1.6] Merge Sort Algorithm in Python

Linear Search in Python

Linear Search is used to search an item in a list or array using linear traversal and comparison with each and every element.
Best Case : O(1)
Worst Case: O(n)

linearsearch.py


# [1.1] Linear Search
arr = [3,6,12,65,2,9] #list / array
item = 12

for i in range(len(arr)):
    if item == arr[i]:
        print(arr[i],i)

# Find number of occurences in a given string
str = "thinkx academy"
element = 'a'
count = 0

for i in range(len(str)):
    if element == str[i]:
        count = count + 1
print(count)


Output


12 2
2